﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShareMarking.Service;
using ShareMarking.Entity;
using ShareMarking.Common;

namespace ShareMarking.Web
{
    /// <summary>
    /// api 的摘要说明
    /// </summary>
    public class geturl : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
           // var method = ComLib.QueryStringHelper.GetFormString("action");
            string action = DTRequest.GetQueryString("action");
            switch (action)
            {
                case "geturl":
                    GetJumpUrl(context);
                    break;               
                default:
                    context.Response.Write("{\"subcode\":\"05\",\"msg\":\"请求方法错误\",\"result\":{}}");
                    break;
            }           
        }
        private void GetJumpUrl(HttpContext context)
        {
            var code = ComLib.QueryStringHelper.GetFormString("c");
            var ip = ComLib.QueryStringHelper.GetFormString("ip");
            var ipadderss = ComLib.QueryStringHelper.GetFormString("ipadderss"); 
            try
            {
                ComLib.LogLib.Log4NetBase.Log("1、活动二维码请求到达");                              
                #region 开始认证
                if (code.IsNullOrEmpty())
                {
                    //context.Response.Write("请求参数错误");
                    context.Response.Write("{\"subcode\":\"02\",\"msg\":\"请求参数错误\",\"result\":{}}");
                }
                else
                {
                    #region 检查code
                    qr_DiyCode qrdiy = new qr_DiyCode();
                    var iscode = qr_DiyCodeService.Instance.Get_IsExist_ByWhere(qr_DiyCode._.scene_id==code);
                    if (iscode)
                    {
                       var qrdiyentity= qr_DiyCodeService.Instance.Get_SingleEntity_byWhere(qr_DiyCode._.scene_id==code);
                       if (qrdiyentity!=null)
                       {
                           #region 返回URL
                           
                           var url = qrdiyentity.source_url;
                           if (url != "" && url != null)
                           {
                               //results results = new Web.results();
                               //results.subcode = "00";
                               //results.msg = "成功";
                               //results.resulturl = url;
                               //string re = ComLib.JsonHelper.JsonSerializer<results>(results);
                               //context.Response.Write(re);
                               ComLib.LogLib.Log4NetBase.Log("2、返回活动二维码成功！::::" + url);
                           }
                           #endregion

                           #region 添加访问记录
                            //var ip = DTRequest.GetIP();//获取ip
                           if (ip.IsNullOrEmpty() || ipadderss.IsNullOrEmpty())
                           {
                               context.Response.Write("{\"subcode\":\"02\",\"msg\":\"请求参数错误\",\"result\":{}}");
                               ComLib.LogLib.Log4NetBase.Log("2、请求参数错误！ip或者ipadderss为空" + ip + ipadderss);
                               //return;
                           }
                           qr_record recode = new qr_record()
                           {
                               qr_DiyCode_id = qrdiyentity.id,
                               Ip = ip,
                               Ipaddress = ipadderss,
                               CreateOn=DateTime.Now
                           };
                          var result= qr_recordService.Instance.Add_Entity(recode);
                          if (result)
                          {
                              //ComLib.LogLib.Log4NetBase.Log("3、扫码记录添加成功::::qr_DiyCode_id：" + recode.qr_DiyCode_id + "//ip=" + recode.Ip + "//Ipaddress=" + recode.Ipaddress);
                          }
                          else
                          {
                              ComLib.LogLib.Log4NetBase.Log("3、扫码记录添加失败！::::qr_DiyCode_id：" + recode.qr_DiyCode_id + "//ip=" + recode.Ip + "//Ipaddress=" + recode.Ipaddress);
                          }
                           #endregion
                       }
                    }
                    else
                    {
                        context.Response.Write("请求参数不存在！");
                        ComLib.LogLib.Log4NetBase.Log("2、请求参数不存在！::::" + code);  
                    }                   
                    #endregion  
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("3.返回活动二维码失败！" + code + ex.Message + ex.StackTrace);
            }
        }

        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    //public class results
    //{
    //    public string subcode;
    //    public string msg;
    //    public string resulturl;
    //}
}